//
//  mttImageGroup.m
//  mtt
//
//  Created by Gerfried Mikusch on 30.09.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "mttImageGroup.h"
#import "mttImage.h"

#pragma mark -
#pragma mark functions


//TODO#2 Funktionen für die Rotation um Pins
CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
{
	
	//Berechnet die Distanz zwischen zwei punkten
	//d = sqrt((p1.x - p2.x)^2 + (p1.y - p2.y)^2)
	
	
	CGFloat dx = point1.x - point2.x;
	CGFloat dy = point1.y - point2.y;
	return sqrt(dx*dx + dy*dy);
};

CGFloat RadiansBetweenThreePoints(CGPoint ce, CGPoint p1, CGPoint p2)
{
	
	//Berechnet den Winkel der Rotation und liefert ihn als radians zurück
	CGFloat a = DistanceBetweenTwoPoints(p1, p2);
	CGFloat b = DistanceBetweenTwoPoints(ce, p1);
	CGFloat	c = DistanceBetweenTwoPoints(ce, p2);	
	
	CGFloat radians = acos((b*b + c*c - a*a)/(2*b*c));
	NSLog(@"Drehrichtung ausgabe: %f center.x: %f center.y %f", p1.x-p2.x-p1.y+p2.y , ce.x, ce.y);
	
	//Bestimmt die Drehrichtung
	//if(p1.x-p2.x-p1.y+p2.y<0){
	if(p1.y-p2.y>0){
	if (p2.x>ce.x){ return radians*(-1);} else
		{return radians; }
	} else {
		if (p2.x>ce.x){ return radians;} else
		{return radians*(-1); }
}
	
}


#pragma mark -
#pragma mark implementation

@implementation mttImageGroup

@synthesize tapRecognizer, doubleTapRecognizer, tapRecTwoFingers, longPressRecognizer, panRecognizer, pinRotationRecognizer, rotationRecognizer, pinchRecognizer, delegate, imageGroupKey, hidePins;

//BOOL hidePins;
CGPoint hitPointWindow;
mttImage *hitImage;
mttImage *rotationImage;
mttPoint *rotationPin;
CGPoint originBeforeTransformation;
CGPoint originAfterTransformation;
NSMutableArray *rotationImages;

#pragma mark -
#pragma mark init & dealloc

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {		
		
		//UIGestureRecognizer *recognizer;
		
		//Bug #1 AutoresizingSubviews funktioniert nur wenn tatsächlich die bounds der gruppe angesprochen werden
		[self setAutoresizesSubviews:NO];
		
		//recognizer für taps mit einem Finger
		self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
		[self addGestureRecognizer:self.tapRecognizer];
		//self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
		self.tapRecognizer.numberOfTouchesRequired = 1;
		self.tapRecognizer.delegate = self;
		//[recognizer release];
		
		//recognizer für doubletaps mit einem Finger
		self.doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
		[self addGestureRecognizer:self.doubleTapRecognizer];
		//self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
		self.doubleTapRecognizer.numberOfTouchesRequired = 1;
		self.doubleTapRecognizer.numberOfTapsRequired = 2;
		self.doubleTapRecognizer.delegate = self;
		//[recognizer release];
		
		
		//recognizer für taps mit zwei Finger
		self.tapRecTwoFingers = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
		[self addGestureRecognizer:self.tapRecTwoFingers];
		//self.tapRecTwoFingers = (UITapGestureRecognizer *)recognizer;
		self.tapRecTwoFingers.numberOfTouchesRequired = 2;
		self.tapRecTwoFingers.delegate = self;
		//[recognizer release];
		
		//recognizer für longpress (touch & hold)
		self.longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
		[self addGestureRecognizer:self.longPressRecognizer];
		//self.longPressRecognizer = (UILongPressGestureRecognizer *) recognizer;
		self.longPressRecognizer.delegate = self;
		//[recognizer release];
		
		//recognizer für pans -> dragging
		self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
		[self addGestureRecognizer:self.panRecognizer];
		//self.panRecognizer = (UIPanGestureRecognizer *)recognizer;
		self.panRecognizer.minimumNumberOfTouches = 2;					//TODO #2 Pan nur noch mit zwei finger, damit sich der panRecognizer und pinRotationRecognizer nicht in die Quere kommen
		self.panRecognizer.delegate = self;
		//[recognizer release];
		
		//TODO #2 PanRecognizer für Rotation um Punkt
		//recognizer für rotation um einen Punkt
		self.pinRotationRecognizer = [[mttRotationRecognizer alloc] initWithTarget:self action:@selector(handlePinRotation:)];
		[self addGestureRecognizer:self.pinRotationRecognizer];
		//self.pinRotationRecognizer = (mttRotationRecognizer *)recognizer;
		//self.pinRotationRecognizer.maximumNumberOfTouches = 1;
		self.pinRotationRecognizer.delegate = self;
		//[self.pinRotationRecognizer setEnabled:NO];
		//[recognizer release];
		
		//recognizer für Rotations
		self.rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
		[self addGestureRecognizer:self.rotationRecognizer];
		//self.rotationRecognizer = (UIRotationGestureRecognizer *)recognizer;
		self.rotationRecognizer.delegate = self;
		//[recognizer release];
		
		//recognizer für pinches -> skalieren
		self.pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
		[self addGestureRecognizer:self.pinchRecognizer];
		//self.pinchRecognizer = (UIPinchGestureRecognizer *)recognizer;
		self.pinchRecognizer.delegate = self;
		//[recognizer release];
		
		
		hidePins = YES;
		
		rotationImages = [[NSMutableArray alloc] init];
		
		
    }
    return self;
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (void)dealloc {
	[tapRecognizer release];
	[tapRecTwoFingers release];
	[longPressRecognizer release];
	[panRecognizer release];
	[rotationRecognizer release];
	[pinchRecognizer release];
    [super dealloc];
}


#pragma mark -
#pragma mark hit testing

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 
{
	
	
	NSArray *mySubviews = self.subviews;
	
	for (int i=mySubviews.count-1; i>=0; i--) {
		//NSLog(@"Search subviews: %i", i);
		
		mttImage *actSub = [mySubviews objectAtIndex:i];
		
		CGPoint receiverPoint = [self convertPoint:point toView:actSub];
		
		if ([actSub hitTest:receiverPoint withEvent:event] != nil) {
			hitImage = actSub;
			return [actSub hitTest:receiverPoint withEvent:event];
		}
		
	}
	
	return nil;
	
}


-(BOOL)isPoint:(CGPoint)point inRect:(CGRect)rect {

	//!!!: Wird im Moment nicht verwendet
	if (point.x >= rect.origin.x && 
		point.x <= (rect.origin.x + rect.size.width) && 
		point.y >= rect.origin.y && 
		point.y <= (rect.origin.y + rect.size.width)) 
	{
		return YES;
	} else {
		return NO;
	}
	
}

-(BOOL)isImageInGroup:(mttImage*) thisImage {
	
	NSArray* mySubviews = [self subviews];
	
	for (int i=0; i<[mySubviews count]; i++) {
		mttImage* actualImage = [mySubviews objectAtIndex:i];
		if (actualImage.imagekey == thisImage.imagekey) {
			return YES;
		}
	}
	
	return NO;
}


#pragma mark -
#pragma mark size & point Operations


-(void)resizeToSubviews {
	
	originBeforeTransformation = self.frame.origin;
	
	
	NSArray *mySubviews = self.subviews;
	
	
	mttImage *firstImg = [mySubviews objectAtIndex:0];
	
	
	CGRect firstRect = [firstImg convertRect:firstImg.bounds toView:self.superview];
	
	CGFloat maxX = firstRect.origin.x;
	CGFloat minX = firstRect.origin.x + firstRect.size.width;
	CGFloat maxY = firstRect.origin.y;
	CGFloat minY = firstRect.origin.y + firstRect.size.height;
	
	
	//Neue Position und Größe der GroupeView ermitteln
	for (int i=0; i<mySubviews.count; i++) {
		
		mttImage *actualImage = [mySubviews objectAtIndex:i];
		
		CGRect actualRect = [actualImage convertRect:actualImage.bounds toView:self.superview];
		
		//Check minX
		if (actualRect.origin.x < minX ) minX = actualRect.origin.x;
		
		//Check maxX
		if ((actualRect.origin.x + actualRect.size.width) > maxX) maxX = actualRect.origin.x + actualRect.size.width;
		
		//Check minY
		if (actualRect.origin.y < minY ) minY = actualRect.origin.y;
		
		//Check maxY
		if ((actualRect.origin.y + actualRect.size.height) > maxY) maxY = actualRect.origin.y + actualRect.size.height;
		
	}
	
	self.frame = CGRectMake(minX, minY, maxX-minX, maxY-minY);
	
	originAfterTransformation = self.frame.origin;
	
	
	//Position der Subviews wieder an die Größenänderung der Gruppenview anpassen!
	//Funktioniert bei Vergrößerung nicht richtig
	CGFloat xDist;
	CGFloat yDist;
	
	if (originBeforeTransformation.x > originAfterTransformation.x) {
		xDist = originBeforeTransformation.x - originAfterTransformation.x;
		yDist = originBeforeTransformation.y - originAfterTransformation.y;
		
	} else {
		xDist = originAfterTransformation.x - originBeforeTransformation.x;
		yDist = originAfterTransformation.y - originBeforeTransformation.y;
	}
	
	
	for (int i=0; i<mySubviews.count; i++) {
		mttImage *actualImage = [mySubviews objectAtIndex:i];
		
		CGRect newFrame = CGRectMake(actualImage.frame.origin.x - xDist, actualImage.frame.origin.y - yDist, actualImage.frame.size.width, actualImage.frame.size.height);
		actualImage.frame = newFrame;
		
	}
	
	
	
	
}

-(void)setAnchorPointBetweenFirstLocation:(CGPoint)firstLocation andSecondLocation:(CGPoint)secondLocation {
	

	CGFloat minX;
	CGFloat maxX;
	CGFloat minY;
	CGFloat maxY;
	
	//Welcher der X bzw. Y Werte ist der kleine. Notwendig für die berechnung des Mittelpunkts
	if (firstLocation.x < secondLocation.x) {
		minX = firstLocation.x;
		maxX = secondLocation.x;
	} else {
		minX = secondLocation.x;
		maxX = firstLocation.x;
	}
	
	if (firstLocation.y < secondLocation.y) {
		minY = firstLocation.y;
		maxY = secondLocation.y;
	} else {
		minY = secondLocation.y;
		maxY = firstLocation.y;
	}
	
	//Berechne Mittelpunkt der beiden touches
	CGFloat newX = minX + (maxX-minX)/2;
	CGFloat newY = minY + (maxY-minY)/2;
	CGPoint newCenter = CGPointMake(newX, newY);
	//NSLog(@"newCenter x:%f, y:%f", newCenter.x, newCenter.y);
	
	
	//Den neuen Mittelpunkt in das Koordinatensystem der Superview konvertieren
	CGPoint newCenterConverted = [self convertPoint:newCenter toView:self.superview];
	//NSLog(@"convertedCenter x:%f, y:%f", newCenterConverted.x, newCenterConverted.y);
	
	//Den neuen Mittelpunkt normalisieren
	CGPoint normNewCenter = CGPointMake(newCenter.x/self.bounds.size.width, newCenter.y/self.bounds.size.height);
	//NSLog(@"normCenter x:%f, y:%f", normNewCenter.x, normNewCenter.y);
	
	//NSLog(@"oldLayerPosition x:%f, y:%f", self.layer.position.x, self.layer.position.y);
	
	
	//Ankerpunkt und neue Position setzen
	self.layer.anchorPoint = normNewCenter;
	
	//Position des Layers neu setzen, da die änderung des anchorpoints diese auch verändert, also wieder zurücksetzen
	self.layer.position = newCenterConverted;
	//NSLog(@"NewlayerPosition x:%f, y:%f", self.layer.position.x, self.layer.position.y);
	
}


#pragma mark -
#pragma mark gesture recognizing

// Ein TAP bringt die Gruppe nach vorne!
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
	if (recognizer.state == UIGestureRecognizerStateRecognized) {
		[delegate bringViewToFront:self];
		[delegate bringOptionViewToFront];
		
		
		
		
		
		//Wenn die pins angezeigt sind, anzeige ausschalten
		/*
		if (!hidePins) {
			
			[self hidePins:YES];
		
			
		}
		 */
		
	}
}


- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {
	
	if (recognizer.state == UIGestureRecognizerStateRecognized) {
		

		
		if (hidePins) {
			[self hideImagePins:NO];
		} else {
			[self hideImagePins:YES];
		}


			


		
		
		/*
		NSArray *mySubviews = [self subviews];
		
		
		//Pin punkte ein/ausblenden
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
			
			
			[actualImage hideSubviews:!hidePins];		//hidePins = YES -> hidden=NO
			
			 
		}
		
		hidePins = !hidePins;
		
		*/
	}
}


// Touch and hold bring das menü!
-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
	CGPoint hitPointImage = [self convertPoint:[recognizer locationInView:self] toView:hitImage];
	
	//Wenn Pin überhaupt möglich ist - also ein Bild darunter erkannt wird.
	if (recognizer.state == UIGestureRecognizerStateBegan && [delegate isPinPossible:hitImage.imagekey atPoint:hitPointImage]) {
		
		
		
		//Actionsheet für weitere pins
		UIActionSheet *longTapMenu = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
		
		CGPoint hitPoint = [recognizer locationInView:self];
		//Nur wenn ein Pin möglich ist
		[longTapMenu addButtonWithTitle:@"Pin"];
		
		
		hitPointWindow = [recognizer locationInView:nil];
		
		//NSLog(@"hitPoint: x: %f, y: %f", hitPoint.x, hitPoint.y);
		[longTapMenu showFromRect:CGRectMake(hitPoint.x, hitPoint.y, 10, 10) inView:self animated:YES];
		[longTapMenu release];
	
		
	}
	
}


// Bewegt die Gruppe
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
	if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
		
		CGPoint translation = [recognizer translationInView:self.superview];
		[self setCenter:CGPointMake(self.center.x + translation.x, self.center.y + translation.y)];
		[recognizer setTranslation:CGPointZero inView:self.superview];
		
	}
	
}


//TODO: #2 Rotation um einen Pin
// Rotiert das Bild um einen Pin

-(void)handlePinRotation:(mttRotationRecognizer *)recognizer {
	
	//NSLog(@"inHandlePinRotation");
	
	BOOL hatKreis = NO;
	
	if ([recognizer state] == UIGestureRecognizerStateBegan) {
		
		//Das RotationImage setzen - Ansonsten kann man nicht unter einem anderen Bild durchrotieren, da ja dann das hitimage neu gesetzt wird.
		rotationImage = hitImage;
		
		//nächstgelegenen Pin rausfinden
		rotationPin = [rotationImage getNearestPinToPoint:[self convertPoint:recognizer.firstLocation toView:hitImage]];
		NSLog(@"--------------------------------------------------------------------------------------PinID: %i", rotationPin.pinID);
		
		//TODO: #2  Hier brauchen wir eine Liste von allen Bilder die an Hitimage hängen (ausser am rotationspin) Liste muss wie Rotationspin global gespeichert werden
		//TODO: #2	Anchorpoint bei allen Bildern auf rotationPin.pinLoc setzen - achtung vorher umrechnen
		
		//Liste befüllen
		rotationImages = [delegate getImagesForRotation:rotationPin atImage:rotationImage];
		
		
		for (int i=0; i<[rotationImages count]; i++) {
			//Anchorpoint bei jedem bild umrechnen und auf pinloc setzen
			mttImage *actualImage = [rotationImages objectAtIndex:i];
			
			//Anchorpoint setzen und in das Koordinatensystem des nächsten Bildes konvertieren
			CGPoint pointtmp = [self convertPoint:rotationPin.pinLoc fromView:rotationImage];
			CGPoint	pointAtImg = [actualImage convertPoint:pointtmp fromView:self];
			[actualImage setAnchorpointTo:pointAtImg];
			//[actualImage setAnchorpointTo:rotationPin.pinLoc];
		}
		
		if ([rotationImages count] == 0) {
			[rotationImage setAnchorpointTo:rotationPin.pinLoc];
		}
		
		
		hatKreis = [delegate findeKreis:rotationPin.pinID];
		
	}
	
	
		
	
	if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
        
		//Rotationswinkel berechnen
		CGFloat rotation = RadiansBetweenThreePoints(rotationPin.pinLoc, [self convertPoint:recognizer.prevLocation toView:rotationImage], [self convertPoint:recognizer.actLocation toView:rotationImage]);
		
		//NSLog(@"Winkel: %f", rotation);
		// TODO: #2 Alle Bilder um den Rotationspin drehen
		//NSLog(@"rotationimages: %@",rotationImages);
		//for(int x;x<[rotationImages count];x++ ){	NSLog(@"rotationimages!!!!!!: %@",[[rotationImages objectAtIndex:x] imagekey]); }
		
		//if ((rotation >= 0 || rotation <= 0) && !hatKreis) {
			
								
			//rotationImage.transform = CGAffineTransformRotate(rotationImage.transform, rotation);
	
		NSLog(@" --------- RotationImages count: %i", [rotationImages count]);
			for (int i=0; i<[rotationImages count]; i++) {
				
				
				NSLog(@" ---------- IN FOR: i=%i", i);
				//Jedes Bild drehen
				mttImage *actualImage = [rotationImages objectAtIndex:i];
				if ((rotation >= 0 || rotation <= 0) && !hatKreis) {
					NSLog(@"---------IN IF----------");
					//[actualImage performRotation:rotation];
					actualImage.transform = CGAffineTransformRotate(actualImage.transform, rotation);
					//actualImage.transform = CGAffineTransformMakeRotation(rotation);
					NSLog(@"----------Transform of Image ID: %@", actualImage.imagekey);
				}
			}
			
			if ([rotationImages count] == 0) {
				rotationImage.transform = CGAffineTransformRotate(rotationImage.transform, rotation);
			}
			
		//}
		//rotationImage.transform = CGAffineTransformRotate(rotationImage.transform, rotation);
		
		
	}
	
	if (recognizer.state == UIGestureRecognizerStateEnded) {
		
		
		for (int i=0; i<[rotationImages count]; i++) {
			mttImage *actualImage = [rotationImages objectAtIndex:i];
			
			CGPoint point = CGPointMake(actualImage.bounds.size.width, actualImage.bounds.size.height);
			
			[actualImage setAnchorPointBetweenFirstLocation:actualImage.bounds.origin andSecondLocation:point];
		}
		//TODO: #2 Anchorpoints bei allen Bildern wieder zurücksetzen
		CGPoint point = CGPointMake(rotationImage.bounds.size.width, rotationImage.bounds.size.height);
		
		
		if ([rotationImages count] == 0) {
			[rotationImage setAnchorPointBetweenFirstLocation:rotationImage.bounds.origin andSecondLocation:point];
		}
		
		[rotationImages removeAllObjects];
		
	}
	
}


// Rotiert die Gruppe
// Bug#1: Eigentlich wird hier nicht mehr die GroupView transformiert sondern die Subviews. Siehe Bugbeschreibung.
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	CGPoint position;
	
	NSArray *mySubviews = self.subviews;
	
	//Beginn der Transformation
	if ([recognizer state] == UIGestureRecognizerStateBegan) {
		//NSLog(@"RotationBegan");
		//Position der Touches ermitteln
		CGPoint firstLocation = [recognizer locationOfTouch:0 inView:self];
		CGPoint secondLocation = [recognizer locationOfTouch:1 inView:self];
		//NSLog(@"fistTouch x:%f, y:%f", firstLocation.x, firstLocation.y);
		//NSLog(@"secondTouch x:%f, y:%f", secondLocation.x, secondLocation.y);
		
		//set new anchorpoint
		[self setAnchorPointBetweenFirstLocation:firstLocation andSecondLocation:secondLocation];
		
		//Anchorpoints der Subviews setzen
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
			
			[actualImage setAnchorPointBetweenFirstLocation:[self convertPoint:firstLocation toView:actualImage] andSecondLocation:[self convertPoint:secondLocation toView:actualImage]];
			
		}
	}
	
	//Laufende Transformation
    if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged) {
        
		//Bug: #1 Hierbei werden die Subviews zwar rotiert aber die größe deren Frames nicht angepasst. -> ursprüngliche Transformation
		//self.transform = CGAffineTransformRotate(self.transform, [recognizer rotation]);
        
		//Bug #1 Transformiert nur die Subviews - so werden die frameGrößen der subviews angepasst.
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
			
			actualImage.transform = CGAffineTransformRotate(actualImage.transform, [recognizer rotation]);
			
		}
		
		
		[recognizer	setRotation:0];
		position = self.layer.position;
		
    }
	
	
	//Ende der Transformation
	if([recognizer state] == UIGestureRecognizerStateEnded) {
		
		CGPoint point = CGPointMake(self.bounds.size.width, self.bounds.size.height);
		
		[self setAnchorPointBetweenFirstLocation:self.bounds.origin andSecondLocation:point];	
		
		
		//Anchorpoint der Subviews zurücksetzen
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
			
			point = CGPointMake(actualImage.bounds.size.width, actualImage.bounds.size.height);
			
			[actualImage setAnchorPointBetweenFirstLocation:actualImage.bounds.origin andSecondLocation:point];
			
		}
	
	}
	

}

// Skaliert die Gruppe
// Bug#1: Eigentlich wird hier nicht mehr die GroupView transformiert sondern die Subviews. Siehe Bugbeschreibung.
- (void)handlePinch:(UIPinchGestureRecognizer *) recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
	NSArray *mySubviews = self.subviews;
	
	//Beginn der Transformation
	if ([recognizer state] == UIGestureRecognizerStateBegan) {
		
		//Position der Touches ermitteln
		CGPoint firstLocation = [recognizer locationOfTouch:0 inView:self];
		CGPoint secondLocation = [recognizer locationOfTouch:1 inView:self];
		//NSLog(@"fistTouch x:%f, y:%f", firstLocation.x, firstLocation.y);
		//NSLog(@"secondTouch x:%f, y:%f", secondLocation.x, secondLocation.y);
	
		//set new anchorpoint
		[self setAnchorPointBetweenFirstLocation:firstLocation andSecondLocation:secondLocation];
	
		//Anchorpoints der Subviews setzen
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
		
			[actualImage setAnchorPointBetweenFirstLocation:[self convertPoint:firstLocation toView:actualImage] andSecondLocation:[self convertPoint:secondLocation toView:actualImage]];
			
		}
		
	}
	
	//Laufende Transformation
	if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
		
		
		//Bug: #1 Hierbei werden die Subviews zwar skaliert aber die größe deren Frames nicht angepasst. -> ursprüngliche Transformation
		//self.transform = CGAffineTransformScale(self.transform, recognizer.scale, recognizer.scale);
		
		//Bug #1 Transformiert nur die Subviews - so werden die frameGrößen der subviews angepasst.
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
			
			actualImage.transform = CGAffineTransformScale(actualImage.transform, recognizer.scale, recognizer.scale);
			
		}
		
		recognizer.scale = 1;
	}
	
	//Ende der Transformation
	if([recognizer state] == UIGestureRecognizerStateEnded) {
		
		CGPoint point = CGPointMake(self.bounds.size.width, self.bounds.size.height);
		
		[self setAnchorPointBetweenFirstLocation:self.bounds.origin andSecondLocation:point];	
		
		//!!!:Größe und Position der GroupView anpassen funktioniert nicht richtig
		//Spätestens beim nächsten Pin stimmt die Größe aber wieder
		//[self resizeToSubviews];
		
		//Anchorpoint der Subviews zurücksetzen
		for (int i=0; i<mySubviews.count; i++) {
			mttImage *actualImage = [mySubviews objectAtIndex:i];
			
			point = CGPointMake(actualImage.bounds.size.width, actualImage.bounds.size.height);
			
			[actualImage setAnchorPointBetweenFirstLocation:actualImage.bounds.origin andSecondLocation:point];
			
		}
		
	}
	
}



- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

	return YES;
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

	// Pan, Pinch und Rotation sollen simultan laufen. Man könnte auch darüber nachdenken das draggen mit zwei fingern zu ermöglichen!
	if (gestureRecognizer == self.pinchRecognizer ||
		gestureRecognizer == self.rotationRecognizer ||
		gestureRecognizer == self.panRecognizer ||
		otherGestureRecognizer == self.pinchRecognizer ||
		otherGestureRecognizer == self.rotationRecognizer ||
		otherGestureRecognizer == self.panRecognizer
		) {
		
			return YES;
	}
	
	if (gestureRecognizer == self.tapRecognizer || 
		gestureRecognizer == self.doubleTapRecognizer ||
		gestureRecognizer == self.pinRotationRecognizer ||
		gestureRecognizer == self.longPressRecognizer ||
		otherGestureRecognizer == self.tapRecognizer ||
		otherGestureRecognizer == self.doubleTapRecognizer ||
		otherGestureRecognizer == self.pinRotationRecognizer ||
		otherGestureRecognizer == self.longPressRecognizer
		) {
			return YES;
	}

	
	/*
	//Pinch und Rotation sollen simultan laufen. Man könnte auch darüber nachdenken das draggen mit zwei fingern zu ermöglichen!
	if (gestureRecognizer == self.pinchRecognizer ||
		gestureRecognizer == self.rotationRecognizer ||
		otherGestureRecognizer == self.pinchRecognizer ||
		otherGestureRecognizer == self.rotationRecognizer
		) {
		return YES;
	}
	*/
	
	
	
	return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
	return YES;
}

-(void)setGestureRecognizersEnabled:(BOOL)enabled {
	
	NSArray *imgRecognizers	= self.gestureRecognizers;
	for (int i=0; i<imgRecognizers.count; i++) {
		[(UIGestureRecognizer *)[imgRecognizers objectAtIndex:i] setEnabled:enabled];
	}
	
}

#pragma mark -
#pragma mark actions

-(void)actionSheet:(UIActionSheet *)longTapMenu clickedButtonAtIndex:(NSInteger)buttonIndex {

		
	switch (buttonIndex) {
			
		//Pin Button
		case 0:
			
			[delegate performPinOnImages:hitPointWindow withCallerId:hitImage.imagekey];
				
			break;
				
	}
}


-(void)hideImagePins:(BOOL)hide {
	
	//hidePins = hide;
	
	NSArray *mySubviews = [self subviews];

	//Pin punkte ein/ausblenden
	for (int i=0; i<mySubviews.count; i++) {
		mttImage *actualImage = [mySubviews objectAtIndex:i];
		
		
		[actualImage hideSubviews:hide];		//hidePins = YES -> hidden=NO
		
		
	}
	
	self.hidePins = hide;
	
}





@end
